package com.yaodian100.ec.memberevent.service.impl;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import com.yaodian100.core.common.dao.ObjectDao;
import com.yaodian100.core.common.dao.impl.CommonCriteria;
import com.yaodian100.core.common.exception.CoreException;
import com.yaodian100.core.common.service.impl.DomainServiceImpl;
import com.yaodian100.ec.coupon.service.CouponService;
import com.yaodian100.ec.member.entity.Bonus;
import com.yaodian100.ec.member.entity.Member;
import com.yaodian100.ec.member.service.MemberService;
import com.yaodian100.ec.memberevent.entity.MemberEvent;
import com.yaodian100.ec.memberevent.entity.MemberGift;
import com.yaodian100.ec.memberevent.service.MemberEventService;

/**
 * 
 * @author apple.chang
 * 
 */
public class MemberEventServiceImpl extends DomainServiceImpl<MemberEvent> implements MemberEventService {

	@Autowired
	private MemberService memberService;

	@Autowired
	private CouponService couponService;

	private ObjectDao<MemberGift> memberGiftDao;

	/*
	 * (non-Javadoc)
	 * 
	 * @seecom.yaodian100.ec.memberevent.service.MemberEventService#saveMemberEvent(com.yaodian100.ec.memberevent.entity.
	 * MemberEvent)
	 */
	@Override
	@Transactional(readOnly = false)
	public MemberEvent saveMemberEvent(MemberEvent entity) throws CoreException {
		return getDao().save(entity);
	}

	/**
	 * 发激活信用
	 */
	@Override
	@Transactional(readOnly = false)
	public MemberEvent getActiveBonus_Coupon(Member member) {
		CommonCriteria criteria = new CommonCriteria();
		criteria.addIn("applySource", Arrays.asList(member.getApplySource().split("_")[0], "000all"));
		criteria.addLe("startDate", member.getCreateDate());
		criteria.addGe("endDate", member.getCreateDate());

		List<MemberEvent> result = getDao().getList(criteria, null);
		if ((null != result) && (result.size() != 0)) {
			MemberEvent me = result.get(0);
			if ("1".equals(me.getType())) {
				if (!couponService.getIsAvailable(me.getCoupon_uuid())) {
					return null;
				}
			}
			return me;
		}
		return null;
	}

	/**
	 * 激活时检查用
	 */
	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.yaodian100.ec.memberevent.service.MemberEventService#checkActiveGift(com.yaodian100.ec.member.entity.Member)
	 */
	@Override
	@Transactional(readOnly = false)
	public MemberEvent checkActiveBonus_Coupon(Member member) {
		CommonCriteria criteria = new CommonCriteria();
		criteria.addIn("applySource", Arrays.asList(member.getApplySource().split("_")[0], "000all"));
		criteria.addLe("startDate", member.getCreateDate());
		criteria.addGe("endDate", member.getCreateDate());
		MemberEvent me = null;
		try {
			List<MemberEvent> result = getDao().getList(criteria, null);
			if ((null != result) && (result.size() != 0)) {
				me = result.get(0);
				if ("2".equals(me.getType())) { // 送购物金
					Bonus bonus = new Bonus();
					bonus.setMemberId(member.getMemberId());
					bonus.setTrxAmount(me.getAmount());
					bonus.setTrxType("EVENT_POINT");
					bonus.setTrxDate(new Date());
					bonus.setDescription(me.getMemo());
					bonus.setStartDate(DateUtils.addDays(new Date(), me.getWaitActivateDays()));
					bonus.setExpireDate(DateUtils.addDays(new Date(), me.getWaitActivateDays() + me.getAvailableDays()));
					DateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
					DateFormat df2 = new SimpleDateFormat("yyyyMMdd");
					Date d = df.parse(df2.format(bonus.getExpireDate()) + "235959");
					bonus.setExpireDate(d);
					bonus = memberService.saveOrUpdateBonus(bonus);
					return me;
				} else if ("1".equals(me.getType())) { // 送coupon
					if (null != couponService.regCoupon(me.getCoupon_uuid(), member.getMemberId())) {
						return me;
					}
				}

			}
		} catch (Exception e) {
			e.printStackTrace();
			if (null != me && "1".equals(me.getType())) { // 送coupon
				couponService.regCoupon(me.getCoupon_uuid(), member.getMemberId());
			}
			throw new CoreException("MemberEventService: " + e.getMessage(), e);
		}
		return null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.yaodian100.ec.memberevent.service.MemberEventService#saveMemberGift(com.yaodian100.ec.memberevent.entity.MemberGift
	 * )
	 */
	@Override
	@Transactional(readOnly = false)
	public MemberGift saveMemberGift(MemberGift entity) throws CoreException {
		return getMemberGiftDao().saveOrUpdate(entity);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.yaodian100.ec.memberevent.service.MemberEventService#getMemberGiftList(int, int,
	 * com.yaodian100.core.common.dao.impl.CommonCriteria, java.lang.String[])
	 */
	@Override
	public List<MemberGift> getMemberGiftList(int firstResult, int maxResults, CommonCriteria criteria, String[] sortOrder)
			throws CoreException {
		List<MemberGift> memberGiftList = getMemberGiftDao().getListPageable(criteria, sortOrder, firstResult, maxResults);
		return memberGiftList;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.yaodian100.ec.memberevent.service.MemberEventService#getMemberGiftListSize(com.yaodian100.core.common.dao.impl
	 * .CommonCriteria)
	 */
	@Override
	public Number getMemberGiftListSize(CommonCriteria criteria) throws CoreException {
		return getMemberGiftDao().getListSize(criteria);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.yaodian100.ec.memberevent.service.MemberEventService#checkActiveGift(com.yaodian100.ec.member.entity.Member)
	 */
	@Override
	public MemberGift checkActiveGift(Member member) {
		CommonCriteria criteria = new CommonCriteria();
		criteria.addIn("applySource", Arrays.asList(member.getApplySource().split("_")[0], "ALLCHANNEL"));
		criteria.addLe("startDate", member.getCreateDate());
		criteria.addGe("endDate", member.getCreateDate());
		criteria.addEq("is_activate", "Y");
		List<MemberGift> result = getMemberGiftDao().getList(criteria, null);
		if ((null != result) && (result.size() != 0)) {
			return result.get(0);
		}
		return null;
	}

	@Override
	public MemberGift getMemberGiftbyId(String id) {
		return getMemberGiftDao().get(id);
	}

	public ObjectDao<MemberGift> getMemberGiftDao() {
		return memberGiftDao;
	}

	public void setMemberGiftDao(ObjectDao<MemberGift> memberGiftDao) {
		this.memberGiftDao = memberGiftDao;
	}
}
